package com.realife.eson.common.laboratory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.realife.eson.common.util.information.MeJSONUtil;
import com.realife.eson.common.util.information.MeYamlUtil;
import org.junit.Test;

import java.io.File;
import java.util.List;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2022/6/18 14:54
 * @description:
 */
public class MeObjTest {

    public static String meJsonRelativePath = "\\src\\main\\java\\com\\realife\\eson\\common\\laboratory\\me.json";
    public static String strArrJsonRelativePath = "\\src\\main\\java\\com\\realife\\eson\\common\\laboratory\\strArr.json";



    @Test//(expected = AxPaasMetaBusinessException.class)
    public void test1() {

        String path = MeYamlUtil.userDirCurrentPath() + meJsonRelativePath;
        System.out.println("path =\n" + path);

        String content = MeYamlUtil.readFileToString(new File(path));
        System.out.println("content =\n" + content);

        /**
         * json当yaml直接用了，这个厉害了
         */
        MeObj meObj = MeYamlUtil.pathToObjectSnake(path, MeObj.class);
        System.out.println("meObj =\n" + meObj);
        MeObj meObj1 = MeYamlUtil.toObjectEso(path, MeObj.class);
        System.out.println("meObj1 =\n" + meObj1);


        /**
         * MeYamlUtil.toObjectJack(yamlStr, MeObj.class); 只支持yaml转对象
         */
        String yamlStr = MeYamlUtil.json2YamlJack(MeYamlUtil.readFileToString(new File(path)));
        File file = MeYamlUtil.getFileFromStringContent(yamlStr);
        String absolutePath = file.getAbsolutePath();
        // C:\Users\ZZGGC\AppData\Local\Temp\temporary4228336898746617001.yaml
        System.out.println("absolutePath =\n" + absolutePath);
        MeObj meObj2 = MeYamlUtil.toObjectJack(yamlStr, MeObj.class);
        System.out.println("meObj2 =\n" + meObj2);


    }


    @Test
    public void test2() {
        String path = MeYamlUtil.userDirCurrentPath() + meJsonRelativePath;
        MeObj meObj = MeYamlUtil.pathToObjectSnake(path, MeObj.class);

        List<MeObj.ItemsDTO> items = meObj.getItems();
        String jsonString = MeJSONUtil.getJSONString(items);
        System.out.println("jsonString =\n" + jsonString);
        List<MeObj.ItemsDTO> itemsDTOS = MeJSONUtil.getT(jsonString, new TypeReference<List<MeObj.ItemsDTO>>() {
        });
        System.out.println("itemsDTOS =\n" + itemsDTOS);


        JSONArray jsonArray = MeJSONUtil.getJSONArray(items);
        System.out.println("jsonArray =\n" + jsonArray);
        List<MeObj.ItemsDTO> t = MeJSONUtil.getT(jsonArray, new TypeReference<List<MeObj.ItemsDTO>>() {
        });
        System.out.println("t =\n" + t);


        List<MeObj.ItemsDTO> itemsDTOS1 = jsonArray.toJavaList(MeObj.ItemsDTO.class);
        System.out.println("itemsDTOS1 =\n" + itemsDTOS1);

        String s = jsonArray.toJSONString();
        System.out.println("s =\n" + s);

        List<MeObj.ItemsDTO> itemsDTOS2 = MeJSONUtil.jsonArr2JavaList(jsonArray, MeObj.ItemsDTO.class);
        System.out.println("itemsDTOS2 =\n" + itemsDTOS2);
    }


    @Test
    public void test3() {
        String path = MeYamlUtil.userDirCurrentPath() + strArrJsonRelativePath;
        String jsonArr = MeYamlUtil.readFileToString(new File(path));
        List<String> stringList = MeJSONUtil.parseArray(jsonArr, String.class);
        System.out.println("stringList =\n" + stringList);// [aaa, bbb, ccc]


        JSONArray jsonArray = MeJSONUtil.getT(jsonArr, new TypeReference<JSONArray>() {
        });
        System.out.println("jsonArray =\n" + jsonArray);// ["aaa","bbb","ccc"]


        JSONArray jsonArray1 = MeJSONUtil.getJSONArray(jsonArr);
        System.out.println("jsonArray1 =\n" + jsonArray1);// ["aaa","bbb","ccc"]



    }


    public static void main(String[] args) {
        String path = MeYamlUtil.userDirCurrentPath();
        // D:\projects\20211122\eson
        System.out.println("path =\n" + path);
    }
}

